<template>
	<view>
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#5098ff" ref="tabs" :list="list" :current="current" @change="change"
					:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition"
				@animationfinish="animationfinish">
				<!-- 待付款 -->
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view v-if="!unpaidGoodsList.length">
								<view class="centre">
									<u-empty text="暂无待付款订单" mode="order"></u-empty>
									<view class="tips">可以去看看有那些想买的</view>
									<view class="btn" @click="toIndex">随便逛逛</view>
								</view>
							</view>
							<view v-else>
								<view class="order" v-for="res in  unpaidGoodsList" :key="res._id">
									<view class="item" v-for="(goods, index) in res.goodsInOrder" :key="index">
										<view class="left">
											<image :src="goods.indexUrl" mode="aspectFill"></image>
										</view>
										<view class="content">
											<view class="title u-line-2">{{ goods.shopName }}</view>
											<view class="type">{{goods.shopDetails}} </view>
											<view class="delivery-time">发货时间 </view>
										</view>
										<view class="right">
											<view class="price">
												￥{{ goods.price }}
												<!-- <text class="decimal">{{ goods.price }}</text> -->
											</view>
											<view class="number">x{{ goods.num }}</view>
										</view>
									</view>
									<view class="total">
										共{{ totalNum(res.goodsInOrder) }}件商品 合计:
										<!-- 共{{ totalNum(res.goodsInOrder) }}件商品 合计: -->
										<text class="total-price">
											￥{{ res.totalFee }}
											<!-- ￥{{ totalPrice(res.goodsInOrder)*totalNum(res.goodsInOrder) }} -->
											<!-- <text
											class="decimal">{{ priceDecimal(totalPrice(res.goodsInOrder)) }}</text> -->
										</text>
									</view>
									<view class="bottom">
										<view class="more">
											<u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon>
										</view>
										<view class="logistics btn" @click="cancelOrder(res.tradeNo)">不想要了</view>
										<view class="evaluate btn" @click="confirmPay(res.tradeNo,res.totalFee)">确认付款
										</view>
									</view>
								</view>
								<!-- <u-loadmore :status="loadStatus[1]" bgColor="#f2f2f2"></u-loadmore> -->
							</view>
						</view>
					</scroll-view>
				</swiper-item>
				<!-- 待发货 -->
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view>
							待开发
						</view>
					</scroll-view>
				</swiper-item>
				<!-- 待收货 -->
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;">
						<view class="page-box">
							<view v-if="!orderGoodsList.length">
								<view class="centre">
									<u-empty text="暂无待收货订单" mode="order"></u-empty>

									<view class="tips">可以去看看有那些想买的</view>
									<view class="btn" @click="toIndex">随便逛逛</view>
								</view>
							</view>
							<view v-else>
								<view class="order" v-for="(res, index) in  orderGoodsList" :key="res._id">
									<view class="item" v-for="(goods, index) in res.goodsInOrder" :key="index">
										<view class="left">
											<image :src="goods.indexUrl" mode="aspectFill"></image>
										</view>
										<view class="content">
											<view class="title u-line-2">{{ goods.shopName }}</view>
											<view class="type">{{goods.shopDetails}} </view>
											<view class="delivery-time">发货时间 </view>
										</view>
										<view class="right">
											<view class="price">
												￥{{ goods.price }}
												<!-- <text class="decimal">.{{ priceDecimal(goods.price) }}</text> -->
											</view>
											<view class="number">x{{ goods.num }}</view>
										</view>
									</view>
									<view class="total">
										共{{ totalNum(res.goodsInOrder) }}件商品 合计:
										<!-- 共{{ totalNum(res.goodsInOrder) }}件商品 合计: -->
										<text class="total-price">
											￥{{ res.totalFee}}
											<!-- <text
												class="decimal">{{ priceDecimal(totalPrice(res.goodsInOrder)) }}</text> -->
										</text>
									</view>
									<view class="bottom">
										<view class="more">
											<u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon>
										</view>
										<view class="logistics btn">查看物流</view>
										<view class="evaluate btn" @click="confirmRecevingGoods(res.tradeNo)">确认收货
										</view>
									</view>
								</view>
								<!-- <u-loadmore :status="loadStatus[1]" bgColor="#f2f2f2"></u-loadmore> -->
							</view>
						</view>
					</scroll-view>
				</swiper-item>
				<!-- 待评价 -->
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(res, index) in  evaluateGoodsList" :key="res._id">

								<view class="item" v-for="(goods, index) in res.goodsInOrder" :key="index">
									<view class="left">
										<image :src="goods.indexUrl" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2">{{ goods.shopName }}</view>
										<view class="type">{{ goods.shopDetails }}</view>
										<!-- <view class="delivery-time">发货时间 {{ item.deliveryTime }}</view> -->
									</view>
									<view class="right">
										<view class="price">
											￥{{ goods.price }}
										</view>
										<view class="number">x{{ goods.num }}</view>
									</view>
								</view>
								<view class="total">
									共{{ totalNum(res.goodsInOrder) }}件商品 合计:
									<!-- 共{{ totalNum(res.goodsInOrder) }}件商品 合计: -->
									<text class="total-price">
										￥{{ res.totalFee }}
										<!-- ￥{{ totalPrice(res.goodsInOrder)*totalNum(res.goodsInOrder) }} -->
										<!-- <text
										class="decimal">{{ priceDecimal(totalPrice(res.goodsInOrder)) }}</text> -->
									</text>
								</view>
								<view class="bottom">
									<view class="more">
										<u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon>
									</view>
									<!-- <view class="logistics btn">查看物流</view> -->
									<view class="exchange btn">卖了换钱</view>
									<view class="evaluate btn">评价</view>
								</view>
							</view>
							<!-- <u-loadmore :status="loadStatus[3]" bgColor="#f2f2f2"></u-loadmore> -->
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [{
						name: '待付款'
					},
					{
						name: '待发货'
					},
					{
						name: '待收货'
					},
					{
						name: '待评价',
						count: 0
					}
				],
				current: 0,
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0,
				loadStatus: ['loadmore', 'loadmore', 'loadmore', 'loadmore'],
				username: '',
				show: false,
				//待付款
				unpaidGoodsList: [],
				//待收货商品数据
				orderGoodsList: [],
				//待评价
				evaluateGoodsList: []
			};
		},
		onLoad() {
			//待付款
			this.getUnPaid()
			//待收货
			this.getWaitRecevingGoods()
			//待评价
			this.getEvaluateList()
		},
		onShow() {

		},
		computed: {
			// 价格小数
			priceDecimal() {
				return val => {
					if (val !== parseInt(val)) return val.slice(-2);
					else return '00';
				};
			},
			// 价格整数
			priceInt() {
				return val => {
					if (val !== parseInt(val)) return val.split('.')[0];
					else return val;
				};
			}
		},
		methods: {
			//待付款
			getUnPaid() {
				const username = uni.getStorageSync('username');
				uniCloud.callFunction({
					name: 'unpaid-goods',
					data: {
						username: username
					}
				}).then(res => {
					this.unpaidGoodsList = res.result
					console.log(this.unpaidGoodsList);
				})
			},
			//取消订单（待付款）
			cancelOrder(tradeNo) {
				let _this = this
				uni.showModal({
					title: '提示',
					content: "确定取消订单吗?",
					success: function(res) {
						if (res.confirm) {
							// console.log('用户点击确定');
							uniCloud.callFunction({
								name: 'remove-order',
								data: {
									tradeNo: tradeNo
								}
							}).then(res => {
								_this.$u.toast('订单已取消')
								_this.getUnPaid()
							})
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
					}
				});
			},
			//确认付款
			confirmPay(tradeNo, totalFee) {
				let _this = this
				uni.showModal({
					title: '提示',
					content: `请支付${totalFee}`,
					success: function(res) {
						if (res.confirm) {
							console.log(tradeNo);
							console.log('用户点击确定');
							uniCloud.callFunction({
								name: 'confirm-pay',
								data: {
									tradeNo: tradeNo
								}
							}).then(res => {
								// console.log(res);
								_this.getUnPaid()
								_this.getWaitRecevingGoods()
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//获取待收货数据
			getWaitRecevingGoods() {
				const username = uni.getStorageSync('username');
				uniCloud.callFunction({
					name: "getWaitGoods",
					data: {
						username: username
					}
				}).then(res => {
					this.orderGoodsList = res.result.data
					// console.log(this.orderGoodsList);
				})
			},
			//确认收货
			confirmRecevingGoods(tradeNo) {
				let _this = this
				uni.showModal({
					title: '提示',
					content: "你确定收货吗?",
					success: function(res) {
						if (res.confirm) {
							console.log(tradeNo);
							uniCloud.callFunction({
								name: 'confirm-get-goods',
								data: {
									tradeNo: tradeNo
								}
							}).then(res => {
								console.log(res);
								_this.getWaitRecevingGoods()
								_this.getEvaluateList()
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				// const username = uni.getStorageSync('username')
				// const db = uniCloud.database();
				// const collection = db.collection('order').where({
				// 	username: username,
				// 	payStatus: 1,
				// 	backgoods: 0
				// }).update({
				// 	acceptgoods: 1
				// })
				// this.$u.toast('收货成功')
				// this.getWaitRecevingGoods()

			},
			//待评价
			getEvaluateList() {
				const username = uni.getStorageSync('username');
				uniCloud.callFunction({
					name: "evaluateList",
					data: {
						username: username
					}
				}).then(res => {
					console.log(res);
					this.evaluateGoodsList = res.result.data
					this.list[3].count = res.result.data.length
					console.log(this.evaluateGoodsList);
					console.log(this.list[3].count);
				})
			},
			//触底;
			reachBottom() {
				// 此tab为空数据
				if (this.current != 2) {
					this.loadStatus.splice(this.current, 1, "loading")
					setTimeout(() => {
						// this.getOrderList(this.current);
					}, 1200);
				}
			},
			// 页面数据
			// getOrderList(idx) {
			// 	for (let i = 0; i < 5; i++) {
			// 		let index = this.$u.random(0, this.dataList.length - 1);
			// 		let data = JSON.parse(JSON.stringify(this.dataList[index]));
			// 		data.id = this.$u.guid();
			// 		this.orderList[idx].push(data);
			// 	}
			// 	this.loadStatus.splice(this.current, 1, "loadmore")
			// },
			// 总价
			totalPrice(item) {
				let price = 0;
				item.map(val => {
					price += val.price;
				});
				return price
				// return price.toFixed(2);
			},
			// 总件数
			totalNum(item) {
				let num = 0;
				item.map(val => {
					num += val.num;
				});
				return num;
			},
			// tab栏切换
			change(index) {
				this.swiperCurrent = index;
				// this.getOrderList(index);
			},
			transition({
				detail: {
					dx
				}
			}) {
				this.$refs.tabs.setDx(dx);
			},
			animationfinish({
				detail: {
					current
				}
			}) {
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			},
			toIndex() {
				this.$u.route({
					type: 'switchTab',
					url: 'pages/index/index'
				})
			},

		}


	};
</script>

<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>

<style lang="scss" scoped>
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				width: 380rpx;

				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {

				// margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;
				color: $u-type-warning-dark;

				// .decimal {
				// 	font-size: 24rpx;
				// 	margin-top: 4rpx;
				// }

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
				color: $u-type-warning-dark;

			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: #5098ff
			}

			.evaluate {
				color: #5098ff;
				border-color: #5098ff;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: #5098ff
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
</style>
